package cn.microservices.cloud.system.web.controller.auth;

import cn.microservices.cloud.common.core.utils.Response;
import cn.microservices.cloud.system.service.manger.AuthManager;
import cn.microservices.cloud.system.web.controller.auth.dto.LoginDTO;
import cn.microservices.cloud.system.web.controller.auth.dto.RegisterDTO;
import cn.microservices.cloud.system.web.controller.auth.vo.RegisterVO;
import cn.microservices.cloud.system.web.controller.auth.vo.UserInfoVO;
import cn.microservices.cloud.system.web.convert.AuthConvert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 授权管理
 */
@RequestMapping("auth")
@RestController
public class AuthController {

    @Resource
    private AuthManager authManger;

    /**
     * 注册
     * @param registerDTO
     * @return
     */
    @RequestMapping("register")
    public Response<RegisterVO> register(@RequestBody RegisterDTO registerDTO){

        return Response.success(AuthConvert.INSTANCE.convert(authManger.register(AuthConvert.INSTANCE.convert(registerDTO))));
    }


    /**
     * 注册
     * @param loginDTO
     * @return
     */
    @RequestMapping("login/unauth")
    public Response<UserInfoVO> login(@RequestBody LoginDTO loginDTO, HttpServletRequest request, HttpServletResponse response){
        cn.microservices.cloud.system.api.dto.LoginDTO lt = AuthConvert.INSTANCE.convert(loginDTO);
        return Response.success(AuthConvert.INSTANCE.convert(authManger.login(lt,response,request)));
    }

}
